define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    //var uService = require('core/services/noticeService');
    //var getTimeToString = uService.getTimeToString;
    $.widget('rk.blogTableCtrl', {
        _params:{},
        _create: function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._listeners();
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.on("click", "[act='blog_write']", function(){
                require.async(["./blogWriteCtrl.js"],function(){
                    rk.dialog({
                        title: rk.i18n('BLOG_WRITE'),
                        width:"930",
                        success: function (contentElem){
                            contentElem.blogWriteCtrl({type:opt.type,gid:opt.gid});//初始化弹框内容的Ctrl
                        },
                        footer: ['cancel', {
                            act: 'ok', visible: true, text: rk.i18n('SAVE')
                        }]
                    });
                });
            });
            elem.on("click", "[act='blog_type_select']", function(){
                elem.find("[act='blog_type_select_menu']").toggle();
                elem.find("[act='blog_type_select_style']").toggleClass('arrow_up');
            });
            var blog_type_click = function(type,button){
                var param = {pageNo:1,pageSize:20};
                var typeId = button.attr("tid");
                param["blog.categoryId"] = typeId;
                elem.find("[act='blog_type_select_menu']").hide();
                elem.find("[act='blog_type_select_style']").removeClass('arrow_up').addClass("arrow_down");
                elem.find("[act='blog_type_select_value']").html(button.html());
                elem.find("[act='blog_type_select_value']").attr("tid",typeId);
                me.loaddata(type,param);
            }
            elem.on("click", "[act='depart_blog_type_value']", function(){
                var button = $(this);
                blog_type_click("departBlog",button)
            });
            elem.on("click", "[act='profile_blog_type_value']", function(){
                var button = $(this);
                blog_type_click("profileBlog",button)
            });
            //elem.on("click", "[act='blog_click_detail']", function(){
            //   //查看详情
            //    var button = $(this);
            //    var bid = button.attr("bid");
            //    rk.oa.popupFinal({param: {bid:bid}, ctrlPath: 'oa/js/blog/blogContentCtrl'})
            //});
            elem.on("click", "[act='blog_del']", function(){
                var button = $(this);
                var bid = button.attr("bid");
                if(!bid){
                    return;
                }
               //删除
                //delete-blog
                rk.confirm(rk.i18n('CRM_CORE_DELELE_CONFIRM'),{okText:rk.i18n('DELETE')}, function () {
                    rk.post("/json/oa_blog/delete-blog.action",{"blog.id":bid},function(json){
                        if(json.status === 0){
                            //var typeId = elem.find("[act='blog_type_select_value']").attr("tid");
                            //var param = {pageNo:1,pageSize:20};
                            me.loaddata(opt.type,me._params);
                            rk.noticeSuccess(rk.i18n('OPERATE_SUCCESS'));
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                    });
                }, function () {
                    //取消
                });
            });
        },
        _init:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var temp = require('oa/tmpl/blog/tpl_blog_table.tpl');
            if(opt.type == "departBlog") {
                rk.post("/json/oa_blog/blog-group-type.action", {}, function (json) {
                    if (json.status === 0) {
                        var html = rk.templateText(temp, json);
                        elem.html(html);
                    } else {
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
            }else if(opt.type == "profileBlog"){
                rk.post("/json/oa_blog/blog-profile-type.action", {}, function (json) {
                    if (json.status === 0) {
                        var html = rk.templateText(temp, json);
                        elem.html(html);
                    } else {
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
            }
            me.loaddata(opt.type);
        },
        loaddata:function(type,param){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if(!param){
                param = {pageNo:1,pageSize:20};
            }
            elem.find("[act='blog_table_rows']").beLoading();
            var showTable = function(json){
                if(json.status === 0){
                    if(json.gridData.length > 0) {
                        var temp = require('oa/tmpl/blog/tpl_blog_table_row.tpl');
                        var listHtml = "";
                        $.each(json.gridData, function (i, dataJson) {
                            //dataJson.createTime = getTimeToString(dataJson.createdAt,json.nowTime,me);
                            var date = new Date(dataJson.createdAt);
                            var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth()+1) : (date.getMonth() + 1) + "";
                            var dd = date.getDate() < 10 ? "0" + date.getDate() : date.getDate() + "";
                            dataJson.createTime = date.getFullYear() + "-" + month + "-" + dd;
                            listHtml += rk.templateText(temp, dataJson);
                        });
                        elem.find("[act='blog_table_rows']").html(listHtml);
                        elem.find('[act="blog_member_page"]').rkPaging({
                            pagenum: json.pageNo,  //（可选）当前页数，默认1
                            pagesize: param.pageSize,  //（可选）当前每页显示条数，默认20
                            rowscount: json.dataCount, //所有数据数
                            onChange: function (pagenum) {
                                var params = param;
                                params.pageNo = pagenum;
                                params.pageSize = param.pageSize;
                                me.loaddata(type, params);
                            }
                        });
                    }else{
                        require.async(["oa/tmpl/blog/tpl_blog_none.tpl"], function (temp) {
                            var html = rk.templateText(temp, {text: rk.i18n('BLOG_NONE')});
                            elem.find("[act='blog_table_rows']").html(html);
                        });
                    }
                }else{
                    rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                }
            };
            if(type === "departBlog") {
                param.groupId = opt.gid;
                me._params = param;
                rk.post("/json/oa_blog/depart-blog-search.action", param, function (json) {
                    showTable(json);
                });
            }else if(type === "profileBlog"){
                param["respondents.id"] = opt.uid;
                param.sidx = "";
                me._params = param;
                rk.post("/json/oa_blog/blog-search.action", param, function (json) {
                    showTable(json);
                });
            }
        }
    });
});
